package cn.kgc.springboot.vue.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.kgc.springboot.vue.commons.QueryCondition;
import cn.kgc.springboot.vue.response.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.springboot.vue.entity.Template;
import cn.kgc.springboot.vue.service.TemplateService;
import cn.kgc.springboot.vue.mapper.TemplateMapper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
* @author mengshun
* @description 针对表【sys_template】的数据库操作Service实现
* @createDate 2024-12-31 09:10:53
*/
@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template>implements TemplateService{


    @Override
    public ResponseResult templatePage(QueryCondition<Template> queryCondition) {

        Page<Template> templatePage = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        String fileName = queryCondition.getSearchParams().getFileName();

        LambdaQueryWrapper<Template> lambda = new QueryWrapper<Template>().lambda();
        lambda.like(StringUtils.isNotBlank(fileName),Template::getFileName,fileName)
                .orderByDesc(Template::getCreateTime);

        baseMapper.selectPage(templatePage,lambda);
        return ResponseResult.success()
                .data("total",templatePage.getTotal())
                .data("tableData",templatePage.getRecords());
    }

    @Override
    public ResponseResult upload(MultipartFile file) {

        if (file.isEmpty()){
            return  ResponseResult.fail().message("请选择文件");
        }

        String originalFilename = file.getOriginalFilename();
        String filenameExtension = org.springframework.util.StringUtils.getFilenameExtension(originalFilename);
        String uuid = IdUtil.simpleUUID();
        String uuidName = uuid+"."+filenameExtension;
        File f = new File("D:\\upload\\springboot\\",uuidName);


        // 获取文件大小
        String strSize =  NumberUtil.decimalFormat("#.##",file.getSize()/1024.0);
        Double size = Double.parseDouble(strSize);
        try {
            file.transferTo(f);
            return  ResponseResult.success()
                    .data("uuid",uuidName)
                    .data("size",size);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult deleteTemplate(String uuid) {

        // 删除数据库中的基本信息
        LambdaQueryWrapper<Template> lambda = new QueryWrapper<Template>().lambda();
        lambda.eq(Template::getUuidName,uuid);
        baseMapper.delete(lambda);

        // 删除本地磁盘文件   TODO  项目部署在服务器环境下时 记得修改磁盘位置
        File f = new File("D:/upload/springboot/" + uuid);
        if (f.exists()){
            f.delete();
        }
        return ResponseResult.success();
    }
}




